package com.metacube.questionbank.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.model.User;
import com.metacube.questionbank.service.impl.AnswerServiceImpl;
import com.metacube.questionbank.service.impl.QuestionServiceImpl;
import com.metacube.questionbank.service.impl.TagServiceImpl;
import com.metacube.questionbank.service.impl.UserServiceImpl;

@Controller
public class TagController {

	@Autowired
	TagServiceImpl tagService;
	@Autowired
	QuestionServiceImpl questionService;
	@Autowired
	AnswerServiceImpl answerService;
	@Autowired
	UserServiceImpl userService;

	/*
	 * @param req This method returns the view of the tags page.The view
	 * contains all the tags.
	 * 
	 * @return view of tags page.
	 */
	@RequestMapping(value = "tags.htm")
	public String showTags(HttpServletRequest req) {
		List<Tag> tags = tagService.getTags();
		req.setAttribute("tags", tags);
		return "tags";
	}

	/*
	 * @param req This method returns the view that contains the questions
	 * related to the tag on which user clicked.
	 * 
	 * @return view of question page.
	 */
	@RequestMapping(value = "tag_questions.htm", method = RequestMethod.GET)
	public String showTagQuestions(HttpServletRequest req) {
		int tagId = Integer.parseInt(req.getParameter("id"));
		List<Question> tagQuestions = tagService.getTagQuestions(tagId);
		List<Integer> answers = answerService.getAnswerCount(tagQuestions);
		List<List<Tag>> tags = tagService.getQuestionTags(tagQuestions);
		List<User> userList = userService.getUserQId(tagQuestions);
		req.setAttribute("questions", tagQuestions);
		req.setAttribute("ansCount", answers);
		req.setAttribute("tags", tags);
		req.setAttribute("userList", userList);
		return "questions";
	}

	/*
	 * @param req This method returns the tags that the user searches in the
	 * search bar.
	 * 
	 * @return view of tags page
	 */
	@RequestMapping(value = "tag_search.htm", method = RequestMethod.GET)
	public String getMatchedTag(HttpServletRequest req) {
		String tagName = req.getParameter("tagfilter");
		List<Tag> tags = tagService.getMatchedTag(tagName);
		req.setAttribute("tags", tags);
		return "tags";
	}

	/*
	 * @param query string user typed in the tag bar of ask question page. This
	 * method returns the tag list related to the string entered by the user
	 * 
	 * @return tagsList list of tags
	 */
	@RequestMapping(value = "/get_tag_list.json", method = RequestMethod.GET, headers = "Accept=*/*")
	public @ResponseBody List<String> getTagList(
			@RequestParam("term") String query) {
		List<String> tagsList = tagService.getMatchedTagsList(query);
		return tagsList;
	}
}